package com.garlic.face.jlf.transaction.balance.cclhelper;

import android.util.Log;


import com.garlic.face.jlf.transaction.balance.database.GoodsWeight;
import com.garlic.face.jlf.transaction.balance.database.ItemExceptions;
import com.garlic.face.jlf.transaction.balance.greendao.gen.GoodsWeightDao;
import com.garlic.face.jlf.transaction.balance.greendao.gen.ItemExceptionsDao;
import com.garlic.face.jlf.utils.LogUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class GoodsHelper {
    private static final String TAG = "GoodsHelper";

    public static List<GoodsWeight> GetApprovedWeight(GoodsWeightDao goodsWeightDao, String barcode, String incode){
        return goodsWeightDao.queryBuilder().whereOr(GoodsWeightDao.Properties.Barcode.eq(barcode), GoodsWeightDao.Properties.Barcode.eq(incode)).where(GoodsWeightDao.Properties.Approvedflag.eq("1")).orderDesc(GoodsWeightDao.Properties.Update_date).list();
    }

    public static void LearnGoodsWeight(GoodsWeightDao dao, GoodsWeight item){
        Log.d(TAG, "LearnGoodsWeight: 00000000000");
        List<GoodsWeight> list = dao.queryBuilder().where(GoodsWeightDao.Properties.Barcode.eq(item.getBarcode()), GoodsWeightDao.Properties.Qty.eq(item.getQty()),
                GoodsWeightDao.Properties.Restype.eq(item.getRestype()), GoodsWeightDao.Properties.Approvedflag.eq("1")).list();
        if(list.isEmpty()){
            Log.d(TAG, "LearnGoodsWeight: 1111111111");
            int count = dao.queryBuilder().where(GoodsWeightDao.Properties.Barcode.eq(item.getBarcode())).list().size();
            Log.d(TAG, "LearnGoodsWeight: count:" + count);
            list = dao.queryBuilder().where(GoodsWeightDao.Properties.Barcode.eq(item.getBarcode()), GoodsWeightDao.Properties.Qty.eq(item.getQty()),
                    GoodsWeightDao.Properties.Restype.eq(item.getRestype()), GoodsWeightDao.Properties.Weight.between(item.getWeight() - 0.005, item.getWeight() + 0.005)).list();
            if(list.isEmpty()){
                Log.d(TAG, "LearnGoodsWeight: 2222222222");
                dao.insert(item);
            }
            else{
                for(int i = 0; i < list.size(); i++){
                    GoodsWeight goodsWeight = list.get(i);
                    goodsWeight.setApprovedflag("1");
                    dao.update(goodsWeight);
                    Log.d(TAG, "LearnGoodsWeight: 3333333333");
                }
            }
        }
    }

    public static List<GoodsWeight> getLikeGoodsWeightByBarcode(GoodsWeightDao dao, String mCurrentBarCode) {
        return dao.queryBuilder().where(GoodsWeightDao.Properties.Barcode.like("%" + mCurrentBarCode + "%"))
                .orderDesc(GoodsWeightDao.Properties.Id).list();
    }

    public static void UpdateGoodsWeight(GoodsWeightDao dao, String barcode, Double weight){
        List<GoodsWeight> list = getLikeGoodsWeightByBarcode(dao, barcode);
        for(int i = 0; i < list.size(); i++){
            GoodsWeight goodsWeight = list.get(i);
            goodsWeight.setWeight(weight);
            dao.update(goodsWeight);
            LogUtils.d(TAG + "UpdateGoodsWeight: 111111", true);
        }

    }

    public static void UpdateApprovedWeight(GoodsWeightDao dao, String barcode){
        List<GoodsWeight> list = dao.queryBuilder().where(GoodsWeightDao.Properties.Barcode.eq(barcode), GoodsWeightDao.Properties.Approvedflag.eq("1"), GoodsWeightDao.Properties.Restype.in(0,1)).list();
        for(int i = 0; i < list.size(); i++){
            GoodsWeight goodsWeight = list.get(i);
            goodsWeight.setApprovedflag("0");
            dao.update(goodsWeight);
            Log.d(TAG, "UpdateApprovedWeight: 111111");
        }
    }

    public static int SelectItemException(ItemExceptionsDao dao, String barcode, String incode){
        String curr = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String startDate = curr + "000000";
        String endDate = curr + "235959";
        return dao.queryBuilder().whereOr(ItemExceptionsDao.Properties.Barcode.eq(barcode), ItemExceptionsDao.Properties.Incode.eq(incode)).where(ItemExceptionsDao.Properties.Entrytime.between(startDate, endDate)).list().size();
    }

    public static void SaveItemException(ItemExceptionsDao dao, ItemExceptions item){
        dao.insert(item);
    }
}
